//
//  ZMSheetView.swift
//  TravelForDriver
//
//  Created by 浪仔 on 2020/9/2.
//  Copyright © 2020 智蚂优服. All rights reserved.
//

import UIKit

class ZMSheetView: XMBaseView {

//    private var resouceArr:[[String]] = []
    private var oneIndex:Int = 0
    private var twoIndex:Int = 0
    private var threeIndex:Int = 0
    private var config = ZMSheetConfig()
    private var zmSheetBlock: ((_ selectIndex: [Int])->())?
    
    @IBOutlet weak var cirView: UIView!
    @IBOutlet weak var pickView: UIPickerView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.frame = SCREEN_BOUNDS
        pickView.delegate = self
        pickView.dataSource = self
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        ZMUtil.clipCircle(view: self.cirView, Corner: [.topLeft,.topRight], radius: 10)
    }
    
    public class func sheet(config: ZMSheetConfig, clickBlock: ((_ selectIndex: [Int])->())?) {
        let v = ZMUtil.loadNibView(nibName: ZMSheetView.self)
        v.frame = SCREEN_BOUNDS
        v.config = config
        v.zmSheetBlock = clickBlock
        v.pickView.reloadAllComponents()
        APPWindow?.addSubview(v)
    }
    
    // 确定点击
    @IBAction func okBtnClick(_ sender: Any) {
        self.removeFromSuperview()
        guard let block = zmSheetBlock else {return}
        block([oneIndex, twoIndex, threeIndex])
    }
    
    // 取消
    @IBAction func cancelBtnClick(_ sender: Any) {
        self.removeFromSuperview()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch: AnyObject in touches {
            let t:UITouch = touch as! UITouch
            let touchPoint = t.location(in: self)
            if self.cirView.frame.contains(touchPoint)  {// 这里排除不需要触发点击事件的范围(rect内)
                print("点击的这块儿不会有反应！")
            }else {
                // 此处进行操作
                self.removeFromSuperview()
            }
        }
    }
}

// MARK: - UIPickerViewDelegate, UIPickerViewDataSource
extension ZMSheetView: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return config.resouceArr.count
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        let subArr:[String] = config.resouceArr[component]
        return subArr.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        let subArr:[String] = config.resouceArr[component]
        return subArr[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return config.cellHeight
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {
            oneIndex = row
        }else if component == 1 {
            twoIndex = row
        }else if component == 2 {
            threeIndex = row
        }
        pickView.reloadAllComponents()
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        //设置分割线
        for view in pickerView.subviews {
            if view.frame.size.height <= 1 {
                view.isHidden = true
            }
        }
        var pickerLabel = view as? UILabel
        if pickerLabel == nil {
            pickerLabel = UILabel()
            pickerLabel?.textAlignment = .center
            pickerLabel?.font = config.defaultFont
            pickerLabel?.textColor = config.defaultColor
            if component == 0 {
                if oneIndex == row {
                    pickerLabel?.font = config.selectFont
                    pickerLabel?.textColor = config.selectColor
                }
            }else if component == 1 {
                if twoIndex == row {
                    pickerLabel?.font = config.selectFont
                    pickerLabel?.textColor = config.selectColor
                }
            }else if component == 2 {
                if threeIndex == row {
                    pickerLabel?.font = config.selectFont
                    pickerLabel?.textColor = config.selectColor
                }
            }
        }
        let subArr:[String] = config.resouceArr[component]
        pickerLabel?.text = subArr[row]
        return pickerLabel!
    }
}


class ZMSheetConfig {
    
    /// 数据源
    var resouceArr:[[String]] = []
    /// 取消按钮背景，默认白色
    public var cancelBtnBgColor: UIColor = .white
    /// 取消按钮文字颜色，默认#CCCCCC
    public var cancelBtnTextColor: UIColor = UIColor.zm.colorCCCCCC()
    /// 取消按钮文字,默认取消
    public var cancelBtnText: String = "取消"
    /// 确定按钮背景，默认白色
    public var sureBtnBgColor: UIColor = .white
    /// 确定按钮背景，默认红色
    public var sureBtnTextColor: UIColor = UIColor.zm.color(hexString: "#F3625D")
    /// 确定按钮文字，默认确定
    public var sureBtnText: String = "确定"
    /// cell高度
    public var cellHeight: CGFloat = 44.0
    /// 默认字体
    public var defaultFont = UIFont.zm.fontRegular(size: 14)
    /// 默认字体颜色
    public var defaultColor = UIColor.zm.color(hexString: "#B2B2B2")
    /// 已选择字体属性
    public var selectFont = UIFont.zm.fontSemibold(size: 14)
    /// 已选择字体颜色
    public var selectColor = UIColor.zm.color333333()
}
